//新增

$("#addForm").validate({
	errorElement : 'span',
	errorPlacement : function(error, element) {
		error.appendTo(element.parent());
	},
	rules : {
		title : "required",
		author : "required",
		content : "required",

	},
	msgs : {
		title : "请输入标题",
		author : "请输入作者",
		content : "请输入内容",

	},
	submitHandler : function(form) {
		$('#saveBtn').attr("disabled",true);
	    setTimeout("$('#saveBtn').removeAttr('disabled')",3000);
		
		 var form = new FormData(document.getElementById("addForm"));
		
		 $.ajax({
                url:"insertIntroGood",
                type:"post",
                dataType:"json",
                data:form,
                processData:false,
                contentType:false,
                success:function(data){
                	if(data.code == 200){
                		setTimeout(function(){
    						layer.msg("添加成功", {icon : 1,time : 1500});
    						$(form).resetForm();
    						layer.closeAll('loading');
    						$("#modal-add").modal('hide');
    						$("#table").bootstrapTable('refresh');
    					},1500);
                	}
                },
                error:function(e){
                	layer.msg("添加失败", {icon : 2,time : 1500,});
					layer.closeAll('loading');
                }
            });       
		layer.load(2);
	},
});








//修改查看
$("#edit").click(function(){
	var ids = getSelections(); 
	var introGood = getSelectionss(); 
	if (ids.length == 1) {
		// 根据id 查询信息 
		$("input[name=e_id]").val(introGood[0].id);
		$("input[name=e_title]").val(introGood[0].title);
		$("input[name=e_time]").val(introGood[0].time);
		$("input[name=e_author]").val(introGood[0].author);
		$("#e_content").text(introGood[0].content);
		$('#modal-edit').modal('toggle');

	}else if(ids.length == 0){
		layer.msg("未选择数据");
	}else{
		layer.msg("请勿勾选多个");
	}
});



//确认修改
$("#editBtn").click(function(){
	
	var id = $("#e_id").val();
	var title = $("#e_title").val();
	var time = $("#e_time").val();
	var author = $("#e_author").val();
	var content = $("#e_content").val();
	
	$.post("updateIntroGoodById",
	   {id : id,title : title,time : time,author : author,content : content},
	   function(data){
		   if(data.code == 200){
				layer.msg(data.msg, {icon : 1,time : 1500});
			}else{
				layer.msg(data.msg, {icon : 2,time : 1500});
			}
	},"json");

});


//删除 deleteBtn
$("#deleteBtn").click(function(){
	var ids = getSelections(); 
	
	if(ids.length == 0){
		layer.msg("未选择数据");
	}else{
		$.post("deleteIntroGoodById",{pIds : JSON.stringify(ids)},function(data){
			if(data.code == 200){
				layer.msg(data.msg, {icon : 1,time : 1500});
			}else{
				layer.msg(data.msg, {icon : 2,time : 1500});
			}
			$('#table').bootstrapTable('remove', {
				field : 'id',
				values : ids,
			});
		},"json");
	}
	
});


function showDeatil(id){
	$.post("introGoodById",{
	id : id
	},function(data){
		$("#id").text(data[0].id);
		$("#title").text(data[0].title);
		$("#time").text(data[0].time);
		$("#author").text(data[0].author);
		$("#content").text(data[0].content);

		str = data[0].imges; //这是一字符串 
		var strs= new Array(); //定义一数组 
		strs=str.split(";"); //字符分割 
		for (i=0;i<strs.length-1 ;i++ ) 
		{ 
//			$("#imageUrl").attr("src","http://127.0.0.1:8088/adicms_image" + strs[i]);
//			document.write(strs[i]+"<br/>"); //分割后的字符输出 
//			console.log(strs[i]+"<br/>");
//			$("#imges").append("<img src=" + "http://127.0.0.1:8088/adicms_image" + strs[i] + " style='width:157px;height:150px;float: left;margin-left: 2px;' class='img-rounded'/>");
			$("#imges").append("<img src=" + "http://47.104.67.69:8088/adicms_image" + strs[i] + " style='width:157px;height:150px;float: left;margin-left: 2px;' class='img-rounded'/>");					
					
		} 
	},"json");
}


function getSelections() {
	return $.map($('#table').bootstrapTable('getSelections'),function(row) {
		return row.id;
	});
}

function getSelectionss() {
	return $.map($('#table').bootstrapTable('getSelections'),function(row) {
		return row;
	});
}

function operating_status(value, row, index) {
	return [
		"<a class='detail' href='javascript:void(0)'>查看详情</a>"
	].join('');	
}


/**
 * 操作列的事件监听
 */
window.operateEvents = {
	'click .detail': function(e, value, row, index) {
		var id = row.id;
		showDeatil(id);
		$('#modal-detail').modal('toggle');
	}
};


